package com.huiyeji.hui.service.impl;

import com.huiyeji.hui.entity.GroupUserRelation;
import com.huiyeji.hui.exception.HuiException;
import com.huiyeji.hui.mapper.GroupUserRelationMapper;
import com.huiyeji.hui.service.GroupRelationService;
import com.huiyeji.hui.utils.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhaoduo
 */
@Service
public class GroupRelationServiceImpl implements GroupRelationService {

    private final GroupUserRelationMapper mapper;

    @Autowired
    public GroupRelationServiceImpl(GroupUserRelationMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void removeMember(Long uid, Long gid) {
        int row = mapper.removeMember(uid,gid);
        if (row != 1){
            throw new HuiException(ErrorEnum.EXIT_GROUP_ERROR);
        }
    }

    @Override
    public void insertGroupOwner(GroupUserRelation relation) {
        int row = mapper.insertSelective(relation);
        if (row != 1){
            throw new HuiException(ErrorEnum.CREATE_ERROR);
        }
    }

    @Override
    public void kickOutMember(Long uid, Long gid, Long oid) {
        int row = mapper.kickOutMember(uid,gid,oid);
        if (row != 1){
            throw new HuiException(ErrorEnum.KICK_OUT_ERROR);
        }
    }

    @Override
    public List<Long> getGroupMembers(Long gid,Long uid) {
        return mapper.getGroupMembers(gid,uid);
    }

    @Override
    public void addRelation(GroupUserRelation relation) {
        int row = mapper.insertSelective(relation);
        if (row != 1){
            throw new HuiException(ErrorEnum.UNABLE_AGREE_INVITATION);
        }
    }

    @Override
    public void removeAllMember(Long gid) {
        mapper.removeAllMember(gid);
    }
}
